package data_types;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Null {
	// JDBC driver name and database URL
	static final String JDBC_DRIVER = "org.sqlite.JDBC";
	static final String DB_URL = "jdbc:sqlite:d:/db/sqlite/test.db";

	public static void main(String[] args) {

		try {
			Class.forName(JDBC_DRIVER);
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		}

		try (Connection con = DriverManager.getConnection(DB_URL);
				PreparedStatement ps = createPreparedStatement(con);
				ResultSet rs = ps.executeQuery();) {

			while (rs.next()) {
				// Retrieve by column name
				int age = rs.getInt("age");
				if (rs.wasNull()) {
					System.out.println("age was null");
				} else {
					System.out.println(age);
				}
			}
		} catch (SQLException se) {
			// Handle errors for JDBC
			se.printStackTrace();
		}

	}

	private static PreparedStatement createPreparedStatement(Connection con)
			throws SQLException {
		// retrieve row with null values
		String sql = "SELECT id, first, last, age FROM emp";
		PreparedStatement ps = con.prepareStatement(sql);
		return ps;
	}
}
